package com.ruoyi.web.controller.aggregation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.service.aggregation.IQrCodeService;

/**
 * 二维码Controller
 * 
 * @author ruoyi
 * @date 2025-01-01
 */
@RestController
@RequestMapping("/agg/qrcode")
public class QrCodeController
{
    @Autowired
    private IQrCodeService qrCodeService;

    /**
     * 生成商家聚合二维码
     */
    @GetMapping("/generate")
    public AjaxResult generate(@RequestParam Long merchantId)
    {
        try
        {
            String qrCodePath = qrCodeService.generateMerchantQrCode(merchantId);
            return AjaxResult.success(qrCodePath);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 生成App下载二维码
     */
    @GetMapping("/download")
    public AjaxResult download()
    {
        try
        {
            String qrCodePath = qrCodeService.generateDownloadQrCode();
            return AjaxResult.success(qrCodePath);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 处理二维码扫描
     */
    @GetMapping("/scan")
    public AjaxResult scan(@RequestParam String code)
    {
        try
        {
            // 解析二维码内容
            // 如果是 merchant://123 格式，返回商家ID
            if (code.startsWith("merchant://"))
            {
                Long merchantId = Long.parseLong(code.replace("merchant://", ""));
                return AjaxResult.success(merchantId);
            }
            
            return AjaxResult.success(code);
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }
}


